﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml.Serialization;

namespace Urbania.GUI.Elements
{
    public enum MenuType { Regular, TileSelection };


    [Serializable]
    public class RightClickMenu : Menu
    {
        protected List<RightClickWorldObject> rightClickItems;
        protected SpriteFont font;
        protected Rectangle bounds;
        protected World.Tile o;
        protected bool beenClicked;
        protected MenuType menuType;

        public bool BeenClicked { get { return this.beenClicked; } }

        public RightClickMenu()
            : base()
        {
            this.rightClickItems = new List<RightClickWorldObject>();
            this.font = Utilities.Loader.GetFont("Font\\default");
            this.bounds = new Rectangle(0, 0, 50, 10);
            this.menuType = MenuType.Regular;
        }

        public RightClickMenu(Vector2 position, Color bg)
            : base(position, bg)
        {
            this.rightClickItems = new List<RightClickWorldObject>();
            this.font = Utilities.Loader.GetFont("Font\\default");
            this.bounds = new Rectangle((int)position.X, (int)position.Y, 50, 10);
            this.menuType = MenuType.Regular;
        }

        public RightClickMenu(Vector2 position, Color bg, MenuType menuType)
            : base(position, bg)
        {
            this.rightClickItems = new List<RightClickWorldObject>();
            this.font = Utilities.Loader.GetFont("Font\\menuFont");
            this.bounds = new Rectangle((int)position.X, (int)position.Y, 50, 10);
            this.menuType = menuType;
        }

        public RightClickMenu(Vector2 position, Color bg, Rectangle bounds)
            : base(position, bg)
        {
            this.rightClickItems = new List<RightClickWorldObject>();
            this.font = Utilities.Loader.GetFont("Font\\default");
            this.bounds = bounds;
            this.menuType = MenuType.Regular;
        }

        public RightClickMenu(Vector2 position, Color bg, string font)
            : base(position, bg)
        {
            this.rightClickItems = new List<RightClickWorldObject>();
            this.font = Utilities.Loader.GetFont(font);
            this.bounds = new Rectangle((int)position.X, (int)position.Y, 50, 10);
            this.menuType = MenuType.Regular;
        }

        public void Generate(World.Tile o)
        {
            switch (this.menuType)
            {
                case MenuType.Regular:
                    rightClickItems.Add(new RightClickWorldObject("Build", false, true));
                    rightClickItems.Add(new RightClickWorldObject("Upgrade"));
                    rightClickItems.Add(new RightClickWorldObject("Remove", false, "Build"));
                    this.bounds.Height = (int)font.MeasureString("A").Y * 3;
                    this.bounds.Width = (int)font.MeasureString("Build  >").X;
                    break;

                case MenuType.TileSelection:
                    Program.lua.LuaVirtual.DoFile(Utilities.Loader.GetScriptFile("Build.lua"));
                    foreach (string s in World.TileTypeHelperClass.TileTypeNames)
                    {
                        rightClickItems.Add(new RightClickWorldObject(s, false, "Build", true));
                    }
                    this.bounds = new Rectangle((int)this.position.X, (int)this.position.Y, (int)font.MeasureString("Homeless Shelter ").X, (int)font.MeasureString("A").Y * rightClickItems.Count);
                    break;
            }
            this.o = o;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Vector2 v = new Vector2(bounds.X, bounds.Y);

            spriteBatch.Draw(Utilities.Loader.GetTex("basicPixel"), this.bounds, Color.Blue);

            foreach (RightClickWorldObject r in rightClickItems)
            {
                r.Draw(v, font, spriteBatch);
                v.Y += font.MeasureString(r.Text).Y;
            }
        }

        public override void Update(GameTime gameTime)
        {
            foreach (RightClickWorldObject r in rightClickItems)
                r.Update(gameTime, o, this);
        }

        public void Remove()
        {
            this.beenClicked = true;
        }
    }
}
